package com.example.controller;

import com.example.client.StockClient;
import com.example.client.UserClient;
import com.example.pojo.Stock;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("index")
public class IndexController 
{
	@Autowired
	StockClient stockClient;
	@Autowired
	UserClient userClient;
	
	@GetMapping("pay")
	@GlobalTransactional @Transactional(rollbackFor = Exception.class)
	public void pay(@RequestParam("uid")Integer uid,@RequestParam("bid")Integer bid)
	{
//		Integer price = stockClient.getPrice(bid);//获取书的价格
		userClient.spend(uid,20);//买故事书花费20
		stockClient.reduce(bid);	//故事书库存减少一本
	}
}
